package starComponent.experiments.expedependencies;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JLabel;

/**
 * @author Charles Perin
 * 9 juin 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class OrientedLabel extends JLabel{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5342656320392404687L;
	public static final int LEFT_ORIENTED = 0;
	public static final int RIGHT_ORIENTED = 1;


	private int orientation;

	public OrientedLabel(String text, int orientation){
		super(text);
		this.orientation = orientation;
	}

	@Override
	public void paintComponent(Graphics g){

		Graphics2D g2d = (Graphics2D)g;

		g2d.setColor(Color.WHITE);
		g2d.fillRect(0, 0, 200, 350);

		double angle = 0;
		if(orientation == LEFT_ORIENTED){
			angle = Math.PI/2;
		}
		else{
			angle = -Math.PI/2;
		}

		g2d.rotate(angle, this.getWidth(), this.getHeight());

		g.setColor(Color.BLACK);

		int nbOfLines = 0;
		String textPart1 = new String(getText());
		String textPart2 = new String(getText());
		String textPart3 = new String(getText());
		String textPart4 = new String(getText());
		String textPart5 = new String(getText());

		int cutIndex = 0;

		if(getText().contains("newLine")){

			cutIndex = textPart1.indexOf("newLine");
			textPart2 = textPart1.substring(cutIndex+"newLine".length());
			textPart1 = textPart1.substring(0, cutIndex);
			nbOfLines++;

			if(textPart2.contains("newLine")){
				cutIndex = textPart2.indexOf("newLine");
				textPart3 = textPart2.substring(cutIndex+"newLine".length());
				textPart2 = textPart2.substring(0, cutIndex);
				nbOfLines++;

				if(textPart3.contains("newLine")){
					cutIndex = textPart3.indexOf("newLine");
					textPart4 = textPart3.substring(cutIndex+"newLine".length());
					textPart3 = textPart3.substring(0, cutIndex);
					nbOfLines++;

					if(textPart4.contains("newLine")){
						cutIndex = textPart4.indexOf("newLine");
						textPart5 = textPart4.substring(cutIndex+"newLine".length());
						textPart4 = textPart4.substring(0, cutIndex);
						nbOfLines++;

						if(textPart5.contains("newLine")){
							cutIndex = textPart5.indexOf("newLine");
							textPart5 = textPart5.substring(0, cutIndex);
							nbOfLines++;

						}
					}
				}
			}
		}

		System.out.println("nbOfLines = "+nbOfLines);
		if(orientation == RIGHT_ORIENTED){
			if(nbOfLines==1){
				g.drawString(textPart1, 120+(this.getWidth())/2, (this.getHeight()/2));
			}
			else if(nbOfLines==2){
				g.drawString(textPart1, 120+(this.getWidth())/2, (this.getHeight()/2));
				g.drawString(textPart2, 120+(this.getWidth())/2, (this.getHeight()/2)+20);
			}
			else if(nbOfLines==3){
				g.drawString(textPart1, 120+(this.getWidth())/2, (this.getHeight()/2));
				g.drawString(textPart2, 120+(this.getWidth())/2, (this.getHeight()/2)+40);
				g.drawString(textPart3, 120+(this.getWidth())/2, (this.getHeight()/2)+80);
			}
			else if(nbOfLines==4){
				g.drawString(textPart1, 120+(this.getWidth())/2, (this.getHeight()/2));
				g.drawString(textPart2, 120+(this.getWidth())/2, (this.getHeight()/2)+40);
				g.drawString(textPart3, 120+(this.getWidth())/2, (this.getHeight()/2)+80);
				g.drawString(textPart4, 120+(this.getWidth())/2, (this.getHeight()/2)+120);
			}
			else if(nbOfLines==5){
				g.drawString(textPart1, 120+(this.getWidth())/2, (this.getHeight()/2));
				g.drawString(textPart2, 120+(this.getWidth())/2, (this.getHeight()/2)+40);
				g.drawString(textPart3, 120+(this.getWidth())/2, (this.getHeight()/2)+80);
				g.drawString(textPart4, 120+(this.getWidth())/2, (this.getHeight()/2)+120);
				g.drawString(textPart5, 120+(this.getWidth())/2, (this.getHeight()/2)+160);
			}
			else{
				System.err.println("trop de lignes dans OrientedLabel");
			}
		}
		else{
			System.out.println("right oriented");
			if(nbOfLines==1){
				g.drawString(textPart1, 120+(this.getWidth())/2, (this.getHeight()/2));
			}
			else if(nbOfLines==2){
				g.drawString(textPart1, -(this.getWidth())/2-30, (this.getHeight()/2)+200);
				g.drawString(textPart2, -(this.getWidth())/2-30, (this.getHeight()/2)+200+40);
			}
			else if(nbOfLines==3){
				g.drawString(textPart1, -(this.getWidth())/2-30, (this.getHeight()/2)+200);
				g.drawString(textPart2, -(this.getWidth())/2-30, (this.getHeight()/2)+200+40);
				g.drawString(textPart3, -(this.getWidth())/2-30, (this.getHeight()/2)+200+80);
			}
			else if(nbOfLines==4){
				g.drawString(textPart1, -(this.getWidth())/2, (this.getHeight()/2)+200);
				g.drawString(textPart2, -(this.getWidth())/2, (this.getHeight()/2)+200+40);
				g.drawString(textPart3, -(this.getWidth())/2, (this.getHeight()/2)+200+80);
				g.drawString(textPart4, -(this.getWidth())/2, (this.getHeight()/2)+200+120);
			}
			else if(nbOfLines==5){
				g.drawString(textPart1, -(this.getWidth())/2, (this.getHeight()/2)+200);
				g.drawString(textPart2, -(this.getWidth())/2, (this.getHeight()/2)+200+40);
				g.drawString(textPart3, -(this.getWidth())/2, (this.getHeight()/2)+200+80);
				g.drawString(textPart4, -(this.getWidth())/2, (this.getHeight()/2)+200+120);
				g.drawString(textPart5, -(this.getWidth())/2, (this.getHeight()/2)+200+160);
			}
			else{
				System.err.println("trop de lignes dans OrientedLabel");
			}
		}


	}

}
